/**
 *  数字转中文
 * @param {*} num  万亿以下
 */
function toChineseNumber(num) {
  // 每4位分割
  const parts = num
    .toString()
    .replace(/(?=(\d{4})+$)/g, ',')
    .split(',')
    .filter(Boolean)
  const map = ['零', '一', '二', '三', '四', '五', '六', '七', '八', '九']
  const units = ['', '十', '百', '千']
  const bigUnits = ['', '万', '亿']

  function _handleZero(str) {
    return str.replace(/零+/g, '零').replace(/零$/, '')
  }
  function _transform(str) {
    let res = ''
    for (let i = 0; i < str.length; i++) {
      let c = map[str[i]]
      let u = units[str.length - i - 1]
      if (c === '零') {
        u = ''
      }
      res += c + u
    }
    // 去结尾和连续的零
    res = _handleZero(res)
    return res
  }
  let result = ''
  for (let i = 0; i < parts.length; i++) {
    let c = _transform(parts[i])
    let u = bigUnits[parts.length - i - 1]
    if (c === '') {
      c = '零'
      u = ''
    }
    result += c + u
  }
  result = _handleZero(result)
  // result = result.replace(/^一十/, '十')
  return result
}

function toBigChineseNumber(num) {
  const cnum = toChineseNumber(num)
  const map = {
    零: '零',
    一: '壹',
    二: '贰',
    三: '叁',
    四: '肆',
    五: '伍',
    六: '陆',
    七: '柒',
    八: '捌',
    九: '玖',
    十: '拾',
    百: '佰',
    千: '仟',
    万: '万',
    亿: '亿',
  }
  return cnum
    .split('')
    .map((c) => map[c])
    .join('')
}

const r = toBigChineseNumber(123400567890)
const b = toChineseNumber(123400567890)
console.log(r) // 壹仟贰佰叁拾肆亿零伍拾陆万柒仟捌佰玖拾
console.log(b) // 一千二百三十四亿零五十六万七千八百九十
